<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html lang="en">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>LCOV - code analysis - src/caffe/util/db_lmdb.cpp</title>
  <link rel="stylesheet" type="text/css" href="../../../gcov.css">
</head>

<body>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="title">LCOV - code coverage report</td></tr>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>

    <tr>
      <td width="100%">
        <table cellpadding=1 border=0 width="100%">
          <tr>
            <td width="10%" class="headerItem">Current view:</td>
            <td width="35%" class="headerValue"><a href="../../../index.html">top level</a> - <a href="index.html">src/caffe/util</a> - db_lmdb.cpp<span style="font-size: 80%;"> (source / <a href="db_lmdb.cpp.func-sort-c.html">functions</a>)</span></td>
            <td width="5%"></td>
            <td width="15%"></td>
            <td width="10%" class="headerCovTableHead">Hit</td>
            <td width="10%" class="headerCovTableHead">Total</td>
            <td width="15%" class="headerCovTableHead">Coverage</td>
          </tr>
          <tr>
            <td class="headerItem">Test:</td>
            <td class="headerValue">code analysis</td>
            <td></td>
            <td class="headerItem">Lines:</td>
            <td class="headerCovTableEntry">13</td>
            <td class="headerCovTableEntry">48</td>
            <td class="headerCovTableEntryLo">27.1 %</td>
          </tr>
          <tr>
            <td class="headerItem">Date:</td>
            <td class="headerValue">2020-09-11 22:50:33</td>
            <td></td>
            <td class="headerItem">Functions:</td>
            <td class="headerCovTableEntry">3</td>
            <td class="headerCovTableEntry">7</td>
            <td class="headerCovTableEntryLo">42.9 %</td>
          </tr>
          <tr>
            <td class="headerItem">Legend:</td>
            <td class="headerValueLeg">            Lines:
            <span class="coverLegendCov">hit</span>
            <span class="coverLegendNoCov">not hit</span>
</td>
            <td></td>
          </tr>
          <tr><td><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
        </table>
      </td>
    </tr>

    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
  </table>

  <table cellpadding=0 cellspacing=0 border=0>
    <tr>
      <td><br></td>
    </tr>
    <tr>
      <td>
<pre class="sourceHeading">          Line data    Source code</pre>
<pre class="source">
<a name="1"><span class="lineNum">       1 </span>            : #ifdef USE_LMDB</a>
<span class="lineNum">       2 </span>            : #include &quot;caffe/util/db_lmdb.hpp&quot;
<span class="lineNum">       3 </span>            : 
<span class="lineNum">       4 </span>            : #include &lt;sys/stat.h&gt;
<span class="lineNum">       5 </span>            : 
<span class="lineNum">       6 </span>            : #include &lt;string&gt;
<span class="lineNum">       7 </span>            : 
<a name="8"><span class="lineNum">       8 </span>            : namespace caffe { namespace db {</a>
<span class="lineNum">       9 </span>            : 
<span class="lineNum">      10 </span><span class="lineCov">          2 : void LMDB::Open(const string&amp; source, Mode mode) {</span>
<span class="lineNum">      11 </span><span class="lineCov">          2 :   MDB_CHECK(mdb_env_create(&amp;mdb_env_));</span>
<span class="lineNum">      12 </span><span class="lineCov">          2 :   if (mode == NEW) {</span>
<span class="lineNum">      13 </span><span class="lineNoCov">          0 :     CHECK_EQ(mkdir(source.c_str(), 0744), 0) &lt;&lt; &quot;mkdir &quot; &lt;&lt; source &lt;&lt; &quot; failed&quot;;</span>
<span class="lineNum">      14 </span>            :   }
<span class="lineNum">      15 </span>            :   int flags = 0;
<span class="lineNum">      16 </span><span class="lineCov">          2 :   if (mode == READ) {</span>
<span class="lineNum">      17 </span>            :     flags = MDB_RDONLY | MDB_NOTLS;
<span class="lineNum">      18 </span>            :   }
<span class="lineNum">      19 </span><span class="lineCov">          2 :   int rc = mdb_env_open(mdb_env_, source.c_str(), flags, 0664);</span>
<span class="lineNum">      20 </span>            : #ifndef ALLOW_LMDB_NOLOCK
<span class="lineNum">      21 </span>            :   MDB_CHECK(rc);
<span class="lineNum">      22 </span>            : #else
<span class="lineNum">      23 </span>            :   if (rc == EACCES) {
<span class="lineNum">      24 </span>            :     LOG(WARNING) &lt;&lt; &quot;Permission denied. Trying with MDB_NOLOCK ...&quot;;
<span class="lineNum">      25 </span>            :     // Close and re-open environment handle
<span class="lineNum">      26 </span>            :     mdb_env_close(mdb_env_);
<span class="lineNum">      27 </span>            :     MDB_CHECK(mdb_env_create(&amp;mdb_env_));
<span class="lineNum">      28 </span>            :     // Try again with MDB_NOLOCK
<span class="lineNum">      29 </span>            :     flags |= MDB_NOLOCK;
<span class="lineNum">      30 </span>            :     MDB_CHECK(mdb_env_open(mdb_env_, source.c_str(), flags, 0664));
<span class="lineNum">      31 </span>            :   } else {
<span class="lineNum">      32 </span>            :     MDB_CHECK(rc);
<span class="lineNum">      33 </span>            :   }
<span class="lineNum">      34 </span>            : #endif
<span class="lineNum">      35 </span><span class="lineCov">          4 :   LOG_IF(INFO, Caffe::root_solver()) &lt;&lt; &quot;Opened lmdb &quot; &lt;&lt; source;</span>
<span class="lineNum">      36 </span><span class="lineCov">          2 : }</span>
<span class="lineNum">      37 </span>            : 
<span class="lineNum">      38 </span><span class="lineCov">          2 : LMDBCursor* LMDB::NewCursor() {</span>
<span class="lineNum">      39 </span>            :   MDB_txn* mdb_txn;
<span class="lineNum">      40 </span>            :   MDB_cursor* mdb_cursor;
<span class="lineNum">      41 </span><span class="lineCov">          2 :   MDB_CHECK(mdb_txn_begin(mdb_env_, NULL, MDB_RDONLY, &amp;mdb_txn));</span>
<span class="lineNum">      42 </span><span class="lineCov">          2 :   MDB_CHECK(mdb_dbi_open(mdb_txn, NULL, 0, &amp;mdb_dbi_));</span>
<span class="lineNum">      43 </span><span class="lineCov">          2 :   MDB_CHECK(mdb_cursor_open(mdb_txn, mdb_dbi_, &amp;mdb_cursor));</span>
<span class="lineNum">      44 </span><span class="lineCov">          4 :   return new LMDBCursor(mdb_txn, mdb_cursor);</span>
<span class="lineNum">      45 </span>            : }
<span class="lineNum">      46 </span>            : 
<span class="lineNum">      47 </span><span class="lineNoCov">          0 : LMDBTransaction* LMDB::NewTransaction() {</span>
<span class="lineNum">      48 </span><span class="lineNoCov">          0 :   return new LMDBTransaction(mdb_env_);</span>
<a name="49"><span class="lineNum">      49 </span>            : }</a>
<span class="lineNum">      50 </span>            : 
<span class="lineNum">      51 </span><span class="lineNoCov">          0 : void LMDBTransaction::Put(const string&amp; key, const string&amp; value) {</span>
<span class="lineNum">      52 </span><span class="lineNoCov">          0 :   keys.push_back(key);</span>
<span class="lineNum">      53 </span><span class="lineNoCov">          0 :   values.push_back(value);</span>
<a name="54"><span class="lineNum">      54 </span><span class="lineNoCov">          0 : }</span></a>
<span class="lineNum">      55 </span>            : 
<span class="lineNum">      56 </span><span class="lineNoCov">          0 : void LMDBTransaction::Commit() {</span>
<span class="lineNum">      57 </span>            :   MDB_dbi mdb_dbi;
<span class="lineNum">      58 </span>            :   MDB_val mdb_key, mdb_data;
<span class="lineNum">      59 </span>            :   MDB_txn *mdb_txn;
<span class="lineNum">      60 </span>            : 
<span class="lineNum">      61 </span>            :   // Initialize MDB variables
<span class="lineNum">      62 </span><span class="lineNoCov">          0 :   MDB_CHECK(mdb_txn_begin(mdb_env_, NULL, 0, &amp;mdb_txn));</span>
<span class="lineNum">      63 </span><span class="lineNoCov">          0 :   MDB_CHECK(mdb_dbi_open(mdb_txn, NULL, 0, &amp;mdb_dbi));</span>
<span class="lineNum">      64 </span>            : 
<span class="lineNum">      65 </span><span class="lineNoCov">          0 :   for (int i = 0; i &lt; keys.size(); i++) {</span>
<span class="lineNum">      66 </span><span class="lineNoCov">          0 :     mdb_key.mv_size = keys[i].size();</span>
<span class="lineNum">      67 </span><span class="lineNoCov">          0 :     mdb_key.mv_data = const_cast&lt;char*&gt;(keys[i].data());</span>
<span class="lineNum">      68 </span><span class="lineNoCov">          0 :     mdb_data.mv_size = values[i].size();</span>
<span class="lineNum">      69 </span><span class="lineNoCov">          0 :     mdb_data.mv_data = const_cast&lt;char*&gt;(values[i].data());</span>
<span class="lineNum">      70 </span>            : 
<span class="lineNum">      71 </span>            :     // Add data to the transaction
<span class="lineNum">      72 </span><span class="lineNoCov">          0 :     int put_rc = mdb_put(mdb_txn, mdb_dbi, &amp;mdb_key, &amp;mdb_data, 0);</span>
<span class="lineNum">      73 </span><span class="lineNoCov">          0 :     if (put_rc == MDB_MAP_FULL) {</span>
<span class="lineNum">      74 </span>            :       // Out of memory - double the map size and retry
<span class="lineNum">      75 </span><span class="lineNoCov">          0 :       mdb_txn_abort(mdb_txn);</span>
<span class="lineNum">      76 </span><span class="lineNoCov">          0 :       mdb_dbi_close(mdb_env_, mdb_dbi);</span>
<span class="lineNum">      77 </span><span class="lineNoCov">          0 :       DoubleMapSize();</span>
<span class="lineNum">      78 </span><span class="lineNoCov">          0 :       Commit();</span>
<span class="lineNum">      79 </span><span class="lineNoCov">          0 :       return;</span>
<span class="lineNum">      80 </span>            :     }
<span class="lineNum">      81 </span>            :     // May have failed for some other reason
<span class="lineNum">      82 </span>            :     MDB_CHECK(put_rc);
<span class="lineNum">      83 </span>            :   }
<span class="lineNum">      84 </span>            : 
<span class="lineNum">      85 </span>            :   // Commit the transaction
<span class="lineNum">      86 </span><span class="lineNoCov">          0 :   int commit_rc = mdb_txn_commit(mdb_txn);</span>
<span class="lineNum">      87 </span><span class="lineNoCov">          0 :   if (commit_rc == MDB_MAP_FULL) {</span>
<span class="lineNum">      88 </span>            :     // Out of memory - double the map size and retry
<span class="lineNum">      89 </span><span class="lineNoCov">          0 :     mdb_dbi_close(mdb_env_, mdb_dbi);</span>
<span class="lineNum">      90 </span><span class="lineNoCov">          0 :     DoubleMapSize();</span>
<span class="lineNum">      91 </span><span class="lineNoCov">          0 :     Commit();</span>
<span class="lineNum">      92 </span>            :     return;
<span class="lineNum">      93 </span>            :   }
<span class="lineNum">      94 </span>            :   // May have failed for some other reason
<span class="lineNum">      95 </span>            :   MDB_CHECK(commit_rc);
<span class="lineNum">      96 </span>            : 
<span class="lineNum">      97 </span>            :   // Cleanup after successful commit
<span class="lineNum">      98 </span><span class="lineNoCov">          0 :   mdb_dbi_close(mdb_env_, mdb_dbi);</span>
<span class="lineNum">      99 </span><span class="lineNoCov">          0 :   keys.clear();</span>
<span class="lineNum">     100 </span><span class="lineNoCov">          0 :   values.clear();</span>
<a name="101"><span class="lineNum">     101 </span>            : }</a>
<span class="lineNum">     102 </span>            : 
<span class="lineNum">     103 </span><span class="lineNoCov">          0 : void LMDBTransaction::DoubleMapSize() {</span>
<span class="lineNum">     104 </span>            :   struct MDB_envinfo current_info;
<span class="lineNum">     105 </span><span class="lineNoCov">          0 :   MDB_CHECK(mdb_env_info(mdb_env_, &amp;current_info));</span>
<span class="lineNum">     106 </span><span class="lineNoCov">          0 :   size_t new_size = current_info.me_mapsize * 2;</span>
<span class="lineNum">     107 </span>            :   DLOG(INFO) &lt;&lt; &quot;Doubling LMDB map size to &quot; &lt;&lt; (new_size&gt;&gt;20) &lt;&lt; &quot;MB ...&quot;;
<span class="lineNum">     108 </span><span class="lineNoCov">          0 :   MDB_CHECK(mdb_env_set_mapsize(mdb_env_, new_size));</span>
<span class="lineNum">     109 </span><span class="lineNoCov">          0 : }</span>
<a name="110"><span class="lineNum">     110 </span>            : </a>
<span class="lineNum">     111 </span>            : }  // namespace db
<span class="lineNum">     112 </span><span class="lineCov">          2 : }  // namespace caffe</span>
<span class="lineNum">     113 </span>            : #endif  // USE_LMDB
</pre>
      </td>
    </tr>
  </table>
  <br>

  <table width="100%" border=0 cellspacing=0 cellpadding=0>
    <tr><td class="ruler"><img src="../../../glass.png" width=3 height=3 alt=""></td></tr>
    <tr><td class="versionInfo">Generated by: <a href="http://ltp.sourceforge.net/coverage/lcov.php" target="_parent">LCOV version 1.12</a></td></tr>
  </table>
  <br>

</body>
</html>
